package
{
	import flash.desktop.*;
	import flash.display.*;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.system.Capabilities;
	import flash.text.*;
	
	import kr.as3.display.*;
	import kr.as3.events.FadeEvent;
	import kr.as3.menu.*;
	import kr.as3.samples.captureyourscreen.FullscreenCaptured;
	import kr.as3.utils.*;

	public class CaptureYourScreenDemo extends Sprite
	{
		public function CaptureYourScreenDemo()
		{
			this.showInfoText();
			this.setTrayIcon();
			
			this.addEventListener( Event.ENTER_FRAME, checkClipboard );
		}
		
		/**
		 * Info Text once at the first time. 
		 */
		private function showInfoText(): void
		{
			var text: TextField = new TextField();
			text.type = TextFieldType.DYNAMIC;
			text.defaultTextFormat = new TextFormat( null, 40, 0x999999 );
			text.text = ( verifyOS() == "mac" ) ? "command + shift + control + 3을 눌러주세요." : "PrntSc 버튼을 눌러주세요.";
			text.autoSize = TextFieldAutoSize.LEFT;
			
			var layer: FadeLayer = new FadeLayer();
			layer.delay = 2;
			layer.addChild( BitmapUtil.convertToBitmap( text ) );
			
			var window: NativeWindow = new NewWindow( NativeWindowType.UTILITY, NativeWindowSystemChrome.NONE, true );
			window.stage.addChild( layer );
			window.activate();
			window.stage.frameRate = 60;
			window.width = layer.width + 50;
			window.height = layer.height + 50;
			
			var screen: Rectangle = Screen.mainScreen.visibleBounds;
			
			window.x = ( screen.width - window.width ) / 2;
			window.y = ( screen.height - window.height ) / 2;
			
			layer.fadeIn();
			layer.addEventListener( FadeEvent.FADE_COMPLETE, hideInfoText );
		}
		
		private function hideInfoText( e: FadeEvent ): void
		{
			var layer: FadeLayer = e.currentTarget as FadeLayer;
			layer.removeEventListener( FadeEvent.FADE_COMPLETE, hideInfoText );
			layer.addEventListener( FadeEvent.FADE_COMPLETE, hideInfoWindow );
			layer.fadeOut();
		}
		
		private function hideInfoWindow( e: FadeEvent ): void
		{
			var layer: FadeLayer = e.currentTarget as FadeLayer;
			
			var window: NativeWindow = layer.stage.nativeWindow;
			window.close();
		}
		
		/**
		 * Set a icon
		 */
		private function setTrayIcon(): void
		{
			var circle: Sprite = SpriteUtil.getCircle( 64, 0xFFCCCC );
			
			var bitmap: BitmapData = BitmapUtil.convertToBitmap( circle ).bitmapData;
			
			var menuXML: XML =
			<menu>
				<menu label="CaptureYourScreen Demo" />
				<menu label="" />
				<menu label="Exit" />
			</menu>;
			
			var menu: XMLNativeMenu = new XMLNativeMenu( this, menuXML );
			menu.addEventListener( XMLNativeMenuEvent.SELECT, menuListener );
			
			if( verifyOS() == "mac" )
			{
				DockIcon( NativeApplication.nativeApplication.icon ).bitmaps = [ bitmap, bitmap, bitmap, bitmap ];
				DockIcon( NativeApplication.nativeApplication.icon ).menu = menu.getNativeMenu();
			}
			else
			{
				SystemTrayIcon( NativeApplication.nativeApplication.icon ).bitmaps = [ bitmap, bitmap, bitmap, bitmap ];
				SystemTrayIcon( NativeApplication.nativeApplication.icon ).menu = menu.getNativeMenu();
			}
		}
		
		private function menuListener( e: XMLNativeMenuEvent ): void
		{
			if( e.label == "Exit" )
				NativeApplication.nativeApplication.exit();
		}
		
		private var previousBitmapData: BitmapData;
		private function checkClipboard( e: Event ): void
		{
			var clipboard: Clipboard = Clipboard.generalClipboard
			
			if( clipboard.hasFormat( ClipboardFormats.BITMAP_FORMAT ) == false )
				return;
			
			var data: BitmapData = clipboard.getData( ClipboardFormats.BITMAP_FORMAT ) as BitmapData;
			
			clipboard.clearData( ClipboardFormats.BITMAP_FORMAT );
			
			new FullscreenCaptured( data );
			
			echo( data.width );
		}
		
		private function verifyOS(): String
		{
			var os: String = Capabilities.os.split( " " )[ 0 ];
			
			return os.toLowerCase();
		}
	}
}
